<!-- 商品发布 - 营销设置 -->
<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-width="130px" :disabled="isDetail">
    <!-- todo 服务承诺暂未实现 -->
    <!-- <div
      v-if="formData.salesChannel?.includes(2)"
      class="p-10px border-rd-5px mb-20px border-1px flex flex-items-center bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      <span>服务承诺</span>
    </div>
    <el-form-item label="商品服务承诺" prop="promiseList" v-if="formData.salesChannel?.includes(2)">
      <div class="w-100% border-rd-5px">
        <el-tag
          effect="dark"
          class="mr-15px"
          size="large"
          closable
          v-for="(item, index) in formData.promiseList"
          :key="item.id"
          @close="formData.promiseList?.splice(index, 1)"
          >{{ item.name }}</el-tag
        >
        <el-button @click="addTag('browseMember')">添加服务承诺</el-button>
      </div>
      <Explaintip
        msg="商品服务标签示例"
        :isPicUrl="true"
        picUrl="https://v5.100cms.com.cn/static/dist/shop/img/shop_tag.png"
      />
    </el-form-item> -->
    <div
      class="p-10px border-rd-5px mb-20px border-1px bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      积分获取设置
    </div>
    <el-form-item label="会员积分获取" prop="memberPointsGain">
      <div class="w-100%">
        <el-radio-group
          v-model="formData.memberPointsGain"
          @change="onMemberchange('memberPointsGain')"
        >
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCT_S1_TYPES)"
            :key="dict.value"
            :value="dict.value"
            :disabled="dict.value == 3 && memberLevelList.length == 0"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <template v-if="formData.memberPointsGain == 3">
      <el-form-item v-if="!formData.specType">
        <SkuListMemberShip
          ref="skuListMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="memberPointsGainEntryConfig"
          :rule-type="'memberPointsGain'"
          :value-type="'memberPointsGainEntry'"
          :membership-level-list="memberLevelList"
          :select-type-list="pointsTypes"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'memberPointsGain'"
            :value-type="'memberPointsGainEntry'"
            :membership-level-list="memberLevelList"
            :select-type-list="pointsTypes"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListMemberShip
            ref="skuListMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="memberPointsGainEntryConfig"
            :rule-type="'memberPointsGain'"
            :value-type="'memberPointsGainEntry'"
            :membership-level-list="memberLevelList"
            :select-type-list="pointsTypes"
          />
        </el-form-item>
      </template>
    </template>
    <el-form-item label="付费会员积分获取" prop="paidmemberPointsGain">
      <div class="w-100%">
        <el-radio-group
          v-model="formData.paidmemberPointsGain"
          @change="onPaidMemberchange('paidmemberPointsGain')"
        >
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCT_P1_TYPES)"
            :key="dict.value"
            :value="dict.value"
            :disabled="dict.value == 3 && paidMemberLevelList.length == 0"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <template v-if="formData.paidmemberPointsGain == 3">
      <el-form-item v-if="!formData.specType">
        <SkuListPaidMemberShip
          ref="skuListPaidMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="paidmemberPointsGainEntryConfig"
          :rule-type="'paidmemberPointsGain'"
          :paid-membership-level-list="paidMemberLevelList"
          :value-type="'paidmemberPointsGainEntry'"
          :select-type-list="pointsTypes"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListPaidMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'paidmemberPointsGain'"
            :paid-membership-level-list="paidMemberLevelList"
            :value-type="'paidmemberPointsGainEntry'"
            :select-type-list="pointsTypes"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListPaidMemberShip
            ref="skuListPaidMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="paidmemberPointsGainEntryConfig"
            :rule-type="'paidmemberPointsGain'"
            :paid-membership-level-list="paidMemberLevelList"
            :value-type="'paidmemberPointsGainEntry'"
            :select-type-list="pointsTypes"
          />
        </el-form-item>
      </template>
    </template>
    <!-- todo: 积分抵扣暂缓 -->
    <!-- <div
      class="p-10px border-rd-5px mb-20px border-1px flex flex-items-center bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      <span>积分抵扣设置</span>
      <Icon class="ml-5px" :icon="'ep:question-filled'" />
      <span class="ml-5px">
        定义最多抵扣中的【最多抵扣】指一次下单最多可抵扣；【多件累计】指允许多件累计抵扣。
      </span>
    </div> -->
    <!-- <el-form-item label="会员积分抵扣" prop="deduMemberIntegral">
      <div class="w-100%">
        <el-radio-group
          v-model="formData.deduMemberIntegral!.selectId"
          @change="onMemberchange('deduMemberIntegral')"
        >
          <el-radio :value="0">关闭</el-radio>
          <el-radio :value="1">不限制抵扣数量</el-radio>
          <el-radio :value="2">自定义最多抵扣</el-radio>
        </el-radio-group>
      </div>
    </el-form-item> -->
    <!-- <template v-if="formData.deduMemberIntegral?.selectId == 2">
      <el-form-item v-if="!formData.specType">
        <SkuListMemberShip
          ref="skuListMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="deduMemberIntegralRuleConfig"
          :rule-type="'deduMemberIntegral'"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'deduMemberIntegral'"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListMemberShip
            ref="skuListMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="deduMemberIntegralRuleConfig"
            :rule-type="'deduMemberIntegral'"
          />
        </el-form-item>
      </template>
    </template> -->

    <!-- <el-form-item label="付费会员积分抵扣" prop="deduPaidmemberIntegral">
      <div class="w-100%">
        <el-radio-group
          v-model="formData.deduPaidmemberIntegral!.selectId"
          @change="onPaidMemberchange('deduPaidmemberIntegral')"
        >
          <el-radio :value="0">关闭</el-radio>
          <el-radio :value="1">不限制抵扣数量</el-radio>
          <el-radio :value="2">自定义最多抵扣</el-radio>
        </el-radio-group>
      </div>
    </el-form-item> -->
    <!-- 
    <template v-if="formData.deduPaidmemberIntegral?.selectId == 2">
      <el-form-item v-if="!formData.specType">
        <SkuListPaidMemberShip
          ref="skuListPaidMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="deduPaidmemberIntegralRuleConfig"
          :rule-type="'deduPaidmemberIntegral'"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListPaidMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'deduPaidmemberIntegral'"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListPaidMemberShip
            ref="skuListPaidMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="deduPaidmemberIntegralRuleConfig"
            :rule-type="'deduPaidmemberIntegral'"
          />
        </el-form-item>
      </template>
    </template> -->
    <div
      class="p-10px border-rd-5px mb-20px border-1px flex flex-items-center bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      <span>余额抵扣设置</span>
      <Icon class="ml-5px" :icon="'ep:question-filled'" />
      <span class="ml-5px">
        定义最多抵扣中的【最多抵扣】指一次下单最多可抵扣；【多件累计】指允许多件累计抵扣。
      </span>
    </div>
    <el-form-item
      label="会员余额抵扣"
      prop="memberWalletRule"
      @change="onMemberchange('memberWalletRule')"
    >
      <div class="w-100%">
        <el-radio-group v-model="formData.memberWalletRule">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCT_B1_TYPES)"
            :key="dict.value"
            :value="dict.value"
            :disabled="dict.value == 3 && memberLevelList.length == 0"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <template v-if="formData.memberWalletRule == 3">
      <el-form-item v-if="!formData.specType">
        <SkuListMemberShip
          ref="skuListMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="memberWalletRuleEntryConfig"
          :rule-type="'memberWalletRule'"
          :value-type="'memberWalletRuleEntry'"
          :membership-level-list="memberLevelList"
          :select-type-list="walletTypes"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'memberWalletRule'"
            :membership-level-list="memberLevelList"
            :value-type="'memberWalletRuleEntry'"
            :select-type-list="walletTypes"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListMemberShip
            ref="skuListMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="memberWalletRuleEntryConfig"
            :rule-type="'memberWalletRule'"
            :membership-level-list="memberLevelList"
            :value-type="'memberWalletRuleEntry'"
            :select-type-list="walletTypes"
          />
        </el-form-item>
      </template>
    </template>

    <el-form-item label="付费会员余额抵扣" prop="paidmemberWalletRule">
      <div class="w-100%">
        <el-radio-group
          v-model="formData.paidmemberWalletRule"
          @change="onPaidMemberchange('paidmemberWalletRule')"
        >
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCT_PB1_TYPES)"
            :key="dict.value"
            :value="dict.value"
            :disabled="dict.value == 3 && paidMemberLevelList.length == 0"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>

    <template v-if="formData.paidmemberWalletRule == 3">
      <el-form-item v-if="!formData.specType">
        <SkuListPaidMemberShip
          ref="skuListPaidMembershipRef"
          :prop-form-data="formData"
          :property-list="propertyList"
          :rule-config="paidmemberWalletRuleEntryConfig"
          :rule-type="'paidmemberWalletRule'"
          :paid-membership-level-list="paidMemberLevelList"
          :value-type="'paidmemberWalletRuleEntry'"
          :select-type-list="walletTypes"
        />
      </el-form-item>
      <template v-if="formData.specType && propertyList.length > 0">
        <el-form-item v-if="!isDetail" label="批量设置">
          <SkuListPaidMemberShip
            :is-batch="true"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-type="'paidmemberWalletRule'"
            :paid-membership-level-list="paidMemberLevelList"
            :value-type="'paidmemberWalletRuleEntry'"
            :select-type-list="walletTypes"
          />
        </el-form-item>
        <el-form-item label="规格列表">
          <SkuListPaidMemberShip
            ref="skuListPaidMembershipRef"
            :is-detail="isDetail"
            :prop-form-data="formData"
            :property-list="propertyList"
            :rule-config="paidmemberWalletRuleEntryConfig"
            :rule-type="'paidmemberWalletRule'"
            :paid-membership-level-list="paidMemberLevelList"
            :value-type="'paidmemberWalletRuleEntry'"
            :select-type-list="walletTypes"
          />
        </el-form-item>
      </template>
    </template>

    <!-- <div
      class="p-10px border-rd-5px mb-20px border-1px flex flex-items-center bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      <span>关联代金券设置</span>
      <Icon class="ml-5px" :icon="'ep:question-filled'" />
      <span class="ml-5px"> 关联代金券设置 </span>
    </div> -->
    <!-- <el-form-item label="代金券设置" prop="voucherSelectId">
      <div class="w-100%">
        <el-radio-group v-model="formData.voucherSelectId" @change="onCouponchange()">
          <el-radio :value="0">不参与</el-radio>
          <el-radio :value="1">统一设置</el-radio>
          <el-radio :value="2">单一设置</el-radio>
        </el-radio-group>
      </div>
    </el-form-item> -->
    <!-- <template v-if="formData.voucherSelectId == 1">
      <el-form-item>
        <div class="w-100%">
          <el-button @click="handleAddCoupon()">添加代金券</el-button>
          <CouponList v-model:multiple-selection="couponList" :is-detail="isDetail" class="mt-20px">
            <template #extension>
              <el-table-column align="center" label="关联数量" prop="number" min-width="168">
                <template #default="{ row }">
                  <el-input-number
                    v-model="row.number"
                    :disabled="isDetail"
                    :min="0"
                    :precision="0"
                    :step="1"
                    class="w-120px!"
                    controls-position="right"
                  />
                </template>
              </el-table-column>
            </template>
          </CouponList>
        </div>
      </el-form-item>
    </template> -->
    <!-- <template v-if="formData.voucherSelectId == 2">
      <el-form-item>
        <div class="w-100%">
          <el-form-item v-if="!formData.specType">
            <SkuListMemberShip
              ref="skuListMembershipRef"
              :prop-form-data="formData"
              :property-list="propertyList"
              :rule-config="ruleConfig"
              :rule-type="'coupon'"
            />
          </el-form-item>
          <template v-if="formData.specType && propertyList.length > 0">
            <el-form-item v-if="!isDetail" label="批量设置">
              <SkuListMemberShip
                :is-batch="true"
                :prop-form-data="formData"
                :property-list="propertyList"
                :rule-type="'coupon'"
              />
            </el-form-item>
            <el-form-item label="规格列表">
              <SkuListMemberShip
                ref="skuListMembershipRef"
                :is-detail="isDetail"
                :prop-form-data="formData"
                :property-list="propertyList"
                :rule-config="ruleConfig"
                :rule-type="'coupon'"
              />
            </el-form-item>
          </template>
          <div class="mt-20px">
            <el-form-item v-if="!formData.specType">
              <SkuListPaidMemberShip
                ref="skuListPaidMembershipRef"
                :prop-form-data="formData"
                :property-list="propertyList"
                :rule-config="ruleConfig"
                :rule-type="'paycoupon'"
              />
            </el-form-item>
            <template v-if="formData.specType && propertyList.length > 0">
              <el-form-item v-if="!isDetail" label="批量设置">
                <SkuListPaidMemberShip
                  :is-batch="true"
                  :prop-form-data="formData"
                  :property-list="propertyList"
                  :rule-type="'paycoupon'"
                />
              </el-form-item>
              <el-form-item label="规格列表">
                <SkuListPaidMemberShip
                  ref="skuListPaidMembershipRef"
                  :is-detail="isDetail"
                  :prop-form-data="formData"
                  :property-list="propertyList"
                  :rule-config="ruleConfig"
                  :rule-type="'paycoupon'"
                />
              </el-form-item>
            </template>
          </div>
        </div>
      </el-form-item> -->
    <!-- </template> -->
  </el-form>

  <!-- <CouponSelect
    ref="couponSelectDialog"
    v-model:multiple-selection="couponList"
    :take-type="CouponTemplateTakeTypeEnum.ADMIN.type"
  >
    <template #extension>
      <el-table-column align="center" label="关联数量" prop="number" min-width="168">
        <template #default="{ row }">
          <el-input-number
            v-model="row.number"
            :min="0"
            :precision="0"
            :step="1"
            class="w-120px!"
            controls-position="right"
          />
        </template>
      </el-table-column>
    </template>
  </CouponSelect> -->
</template>
<script lang="ts" setup>
import type { Spu } from '@/api/mall/product/spu'
import { PropType } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { copyValueToTarget } from '@/utils'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import {
  getPropertyList,
  PropertyAndValues,
  RuleConfig,
  SkuListMemberShip,
  SkuListPaidMemberShip
  // CouponList
} from '@/views/mall/product/spu/components/index'
defineOptions({ name: 'ProductPromotionForm' })
// /**优惠券功能 */
// // 优惠券列表
// import CouponSelect from '@/views/mall/promotion/coupon/components/CouponSelect.vue'
// import * as CouponTemplateApi from '@/api/mall/promotion/coupon/couponTemplate'
// import { CouponTemplateTakeTypeEnum } from '@/utils/constants'

// const couponList = ref<CouponTemplateApi.CouponTemplateVO[]>([])
// const couponSelectDialog = ref()
// 添加优惠券
// const handleAddCoupon = () => {
//   couponSelectDialog.value.open()
// }
const memberPointsGainEntryConfig: RuleConfig[] = []
// const deduMemberIntegralRuleConfig: RuleConfig[] = []
const memberWalletRuleEntryConfig: RuleConfig[] = []
// const memberDiscountRuleConfig: RuleConfig[] = []
const paidmemberPointsGainEntryConfig: RuleConfig[] = []
// const deduPaidmemberIntegralRuleConfig: RuleConfig[] = []
const paidmemberWalletRuleEntryConfig: RuleConfig[] = []
// const paidmemberDiscountRuleConfig: RuleConfig[] = []

const message = useMessage() // 消息弹窗

const props = defineProps({
  propFormData: {
    type: Object as PropType<Spu>,
    default: () => {}
  },
  activeName: propTypes.string.def(''),
  isDetail: propTypes.bool.def(false), // 是否作为详情组件
  memberLevelList: {
    type: Array as PropType<any[]>,
    default: () => []
  },
  paidMemberLevelList: {
    type: Array as PropType<any[]>,
    default: () => []
  }
})

const formRef = ref() // 表单Ref
const propertyList = ref<PropertyAndValues[]>([]) // 商品属性列表
const skuListMembershipRef = ref() // 商品属性列表 Ref
const skuListPaidMembershipRef = ref()
const pointsTypes = [
  {
    typeName: '固定积分',
    typeCode: 1
  },
  {
    typeName: '积分比例',
    typeCode: 0
  }
]
const walletTypes = [
  {
    typeName: '最多抵扣',
    typeCode: 2
  },
  {
    typeName: '多件累计',
    typeCode: 5
  }
]
// 表单数据
const formData = ref<Spu>({
  specType: false, // 商品规格
  skus: [],
  promiseList: [], //服务承诺
  salesChannel: [], //销售渠道
  memberPointsGain: 0, //会员积分获取
  memberPointsGainEntry: {}, //会员积分获取单独设置
  paidmemberPointsGain: 0, //付费会员积分获取
  paidmemberPointsGainEntry: {}, //付费会员积分获取单独设置,
  memberWalletRule: 0, //会员余额抵扣
  memberWalletRuleEntry: {}, //会员抵扣单独设置,
  paidmemberWalletRule: 0, //付费会员余额抵扣
  paidmemberWalletRuleEntry: {} //付费会员余额抵扣单独设置
  // memberIntegral: {
  //   selectId: 0,
  //   ruleList: []
  // }, //会员积分设置
  // paidmemberIntegral: {
  //   selectId: 0,
  //   ruleList: []
  // }, //付费会员积分设置
  // deduMemberIntegral: {
  //   selectId: 0,
  //   ruleList: []
  // },
  // deduPaidmemberIntegral: {
  //   selectId: 0,
  //   ruleList: []
  // },
  // memberBalance: {
  //   selectId: 0,
  //   ruleList: []
  // },
  // paidmemberBalance: {
  //   selectId: 0,
  //   ruleList: []
  // },
  // voucherSelectId: 0
})
// 表单规则
const rules = reactive({})
/**会员选择改变 */
const onMemberchange = (type: any) => {
  // Object.assign(props.propFormData, formData.value)
}
const onPaidMemberchange = (type: any) => {
  // Object.assign(props.propFormData, formData.value)
}
// const onCouponchange = () => {
//   couponList.value = []
//   onMemberchange('coupon')
//   onPaidMemberchange('paycoupon')
// }

/**付费会员选择改变 */

/** 将传进来的值赋值给 formData */
watch(
  () => props.propFormData,
  (data) => {
    if (!data) {
      return
    }
    copyValueToTarget(formData.value, data)
    // 将 SKU 的属性，整理成 PropertyAndValues 数组
    propertyList.value = getPropertyList(data)
  },
  {
    deep: true,
    immediate: true
  }
)
watch(
  () => formData.value,
  () => {
    Object.assign(props.propFormData, formData.value)
  },
  {
    deep: true,
    immediate: true
  }
)
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
  if (!formRef) return
  try {
    skuListMembershipRef.value?.validateSku()
    skuListPaidMembershipRef.value?.validateSku()
    await unref(formRef)?.validate()
    // 保存时剔除不必要的字段
    if (formData.value.memberPointsGain != 3) {
      formData.value.memberPointsGainEntry = {}
    }
    if (formData.value.paidmemberPointsGain != 3) {
      formData.value.paidmemberPointsGainEntry = {}
    }
    if (formData.value.memberWalletRule != 3) {
      formData.value.memberWalletRuleEntry = {}
    }
    if (formData.value.paidmemberWalletRule != 3) {
      formData.value.paidmemberWalletRuleEntry = {}
    }

    // 校验通过更新数据
    Object.assign(props.propFormData, formData.value)
  } catch (e) {
    message.error('【营销设置】不完善，请填写相关信息')
    emit('update:activeName', 'promotion')
    throw e // 目的截断之后的校验
  }
}
defineExpose({ validate })
</script>
